import {CHANGE_LANGUAGE,ADD_LANGUAGE,LanguageActionTypes} from './languageActions';
import i18n from 'i18next';

interface languageState{
    language:'en' | 'zh';
    languageList:{name:string,code:string}[]
}

const defaultstate:languageState={
    language:'zh',
    languageList:[
        {name:'中文',code:'zh'},
        {name:'English',code:'en'}
    ]
}

export default (state=defaultstate,action:LanguageActionTypes) =>{
    switch (action.type) {
        case CHANGE_LANGUAGE:
          i18n.changeLanguage(action.payload)
          return { ...state, language: action.payload };
        case ADD_LANGUAGE:
          return { ...state, languageList: [...state.languageList, action.payload] };
        default: 
          return state
      }
    
}